///2024/5/24-(v1.08)-重新修改秒杀功能.

#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
#include <sdktools>
#include <sdkhooks>


#define PLUGIN_NAME                   "l4d2_witch_damage_scopekill"    //这里写插件名字
#define PLUGIN_AUTHOR                 "豆瓣酱な"                         //作者
#define PLUGIN_DESCRIPTION            "witch 秒杀范围"   //介绍
#define PLUGIN_VERSION                "1.1"
#define PLUGIN_URL                    "https://gitee.com/sweet-and-sour-bombs"  //网站

public Plugin myinfo =
{
    name        = PLUGIN_NAME,
    author      = PLUGIN_AUTHOR,
    description = PLUGIN_DESCRIPTION,
    version     = PLUGIN_VERSION,
    url         = PLUGIN_URL
}

Handle hCvar_witch_killsxcvxvsvt, hCvar_witch_killddggdgdg;
float witch_killssvt, witch_killdist, g_fDamage;

int  g_witchkillffgg;
ConVar hCvar_witchasdasdad, g_hCvarDamage;




public void OnPluginStart()
{
	RegAdminCmd("sm_ms", Command_SmokerSwitch, ADMFLAG_KICK, "秒杀插件总开关");
	RegAdminCmd("sm_witchms", Command_SmokerSwitch, ADMFLAG_KICK, "witch秒杀范围指令开关");
	hCvar_witchasdasdad = CreateConVar("l4d2_witch_damage_killes", "1", "是否启用witch秒杀范围插件", FCVAR_NOTIFY, true, 0.0, true, 1.0);
	HookConVarChange(hCvar_witchasdasdad, eConvarChanged);	

	HookEvent("witch_spawn", Event_Witch_Spawn, EventHookMode_Pre); //女巫出生事件

	
	hCvar_witch_killsxcvxvsvt = CreateConVar("l4d2_hCvar_witch_killsvxvsvt", "1.0", "witch每多少秒发动秒杀技能?", FCVAR_NOTIFY);
	HookConVarChange(hCvar_witch_killsxcvxvsvt, eConvarChanged);
	
	hCvar_witch_killddggdgdg = CreateConVar("l4d2_hCvar_witch_killddgdg", "120.0", "玩家与witch多少距离?", FCVAR_NOTIFY);
	HookConVarChange(hCvar_witch_killddggdgdg, eConvarChanged);
	
	g_hCvarDamage = CreateConVar("l4d2_meteor_witch_damage", "5000.0", "伤害*2次.", FCVAR_NOTIFY, true, 0.0);	
	HookConVarChange(g_hCvarDamage, eConvarChanged);	
	
	//AutoExecConfig(true, "l4d2_witch_damage_scopekill");
}

public void OnMapStart()
{
	CvarsChanged();
}

public void eConvarChanged(Handle hCvar, const char[] sOldVal, const char[] sNewVal)
{
	CvarsChanged();
}

void CvarsChanged()
{
	witch_killssvt = GetConVarFloat(hCvar_witch_killsxcvxvsvt);
	witch_killdist = GetConVarFloat(hCvar_witch_killddggdgdg);
	g_witchkillffgg = hCvar_witchasdasdad.IntValue;
	g_fDamage = g_hCvarDamage.FloatValue;	
}




public Action Command_SmokerSwitch(int client, int args)
{
	if (g_witchkillffgg)
	{
		hCvar_witchasdasdad.IntValue = 0;
		PrintToChat(client, "\x04[提示]\x03女巫\x05范围秒杀插件已\x03关闭\x05.");
	}
	else
	{
		hCvar_witchasdasdad.IntValue = 1;
		PrintToChat(client, "\x04[提示]\x03女巫\x05范围秒杀插件已\x03开启\x05.");
	}
	return Plugin_Handled;
}

void Event_Witch_Spawn(Event event, const char[] name, bool dontBroadcast)
{
	if(!hCvar_witchasdasdad.BoolValue)
		return;

	int client = event.GetInt("witchid");
	
	
	CreateTimer(witch_killssvt, nexthealing_inf,  client, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);	

		
}

public Action nexthealing_inf(Handle timer, any client)
{

	if(IsWitch(client))
	{


		float pos[3];
		float entpos[3];
		float distance;

		GetEntPropVector(client, Prop_Send, "m_vecOrigin", pos);
		for (int i = 1; i <= MaxClients; i++)
		{
			if (!IsSurvivor(i) || !IsPlayerAlive(i))
				continue;
			GetEntPropVector(i, Prop_Send, "m_vecOrigin", entpos);
			distance = GetVectorDistance(pos, entpos);
			if (distance<=witch_killdist)
			{
				HurtEntity(i, client, g_fDamage);
				HurtEntity(i, client, g_fDamage);
			}
		}
	}
	return Plugin_Continue;
}

stock void HurtEntity(int victim, int client, float damage)
{
	SDKHooks_TakeDamage(victim, client, client, damage, DMG_SLASH);
}




bool IsSurvivor(int client)
{
	return client > 0 && client <= MaxClients && IsClientInGame(client) && GetClientTeam(client) == 2;
}


bool IsWitch(int entity)
{
	if (entity > MaxClients && IsValidEntity(entity) && g_witchkillffgg == 1)
	{
		static char classname[16];
		GetEdictClassname(entity, classname, sizeof(classname));
		if (strcmp(classname, "witch", false) == 0)
			return true;
	}
	return false;
}





